﻿using System;
using System.Data;
using System.Data.Common;
using QzEbook.Common.Emum;
using QzEbook.Common;

/// <summary>
///User 的摘要说明
/// </summary>
/// 

namespace QzEbook.Admin
{
    [Serializable]
    public class SessionUser:abstractAccess<SessionUser>
    {
        public static readonly string LOGINUSER = "User";
        private string name;
        private string passWord;

        public string PassWord
        {
            get { return passWord; }
            set { passWord = value; }
        }
        private string lastIp;
        private string lastDate;
        private int uid;
        private int level;

        #region 属性;
        public string LastDate
        {
            get { return lastDate; }
            set { lastDate = value; }
        }
        public int Level
        {
            get { return level; }
            set { level = value; }
        }

        public int Uid
        {
            get { return uid; }
            set { uid = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string LastIp
        {
            get { return lastIp; }
            set { lastIp = value; }
        }
        #endregion

        public SessionUser()
        {
        }

        /// <summary>
        /// 用户登陆;
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="Ip">IP地址</param>
        /// <returns></returns>
        public LoginAction login(string name, string pwd,string Ip)
        {
            this.Name = name;
            SessionUser userSession= this.select();
            if (userSession==null)
            {
                return LoginAction.UserNoFound;
            }

            //如果存在,绑定回来.
            this.Uid = userSession.Uid;
            this.LastDate = userSession.LastDate;
            this.LastIp = userSession.LastIp;

            if (!string.Equals(pwd,userSession.PassWord))
            {
                return LoginAction.PasswordError;
            }

            DataAccess.ExecuteNonQuery(string.Format("UPDATE Qz_User SET UIp='{0}',ULastDate='{1}' Where UID={2}", Ip,DateTime.Now,Uid));

            return LoginAction.Success;
        }


        public bool ChangePassWord(string newpass, string oldpass)
        {
            DbCommand command = CreateCommand("ChangePassWord");
            addParameter(command, "@UID", Uid);
            addParameter(command, "@oldpass", oldpass);
            addParameter(command, "@newpass", newpass);
            return DataAccess.ExecuteNonQuery(command) > 0;
        }

        protected override SessionUser bindRow(DataRow row)
        {
            SessionUser userSession = new SessionUser();
            
            userSession.Uid = int.Parse(row["UID"].ToString());
            userSession.Name = row["UName"].ToString();
            //userSession.Level = row["ULevel"].ToString();
            userSession.PassWord = row["UPwd"].ToString();
            userSession.LastIp = row["UIp"].ToString();
            userSession.LastDate = row["ULastDate"].ToString();
            
            return userSession;
        }

        protected override DataTable selectTable()
        {
            DbCommand command = CreateCommand("Login");
            addParameter(command, "@UName", this.Name);
            DataTable table = null;
            try
            {
                table = DataAccess.ExcuteSelectCommand(command);
            }
            catch
            {
            }
            return table;
        }
    }
}